class Solution {
public:
    vector<vector<int>> ans;
    vector<int> res;
    vector<int> idx;
    void dfs(int begin,int end,vector<int>& nums,int index){
      if(index == end){
        ans.push_back(res);
        return;
      }
      for(int i = 0;i < end;i++){
        if(idx[i]!=0||i>0&&nums[i]==nums[i-1]&&idx[i-1]==0){
          continue;
        }
        res.push_back(nums[i]);
        idx[i] = 1;
        dfs(0,end,nums,index+1);

        idx[i] = 0;
        res.pop_back();
      }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(begin(nums),end(nums));
        idx.resize(nums.size());
        dfs(0,nums.size(),nums,0);
        return ans;
    }
};
